package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.system.domain.PortStatistics;
import com.ruoyi.system.domain.ReadPer;
import com.ruoyi.system.domain.UnusualExport;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.WorkLog;
import com.ruoyi.system.service.IWorkLogService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 跟踪日志Controller
 * 
 * @author ZYT
 * @date 2020-08-31
 */
@RestController
@RequestMapping("/system/WorkLog")
public class WorkLogController extends BaseController
{
    @Autowired
    private IWorkLogService workLogService;

    /**
     * 查询跟踪日志列表
     */
    @PreAuthorize("@ss.hasPermi('system:WorkLog:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkLog workLog)
    {	
    	System.out.println(workLog.toString());
    	System.out.println("test..........");
        startPage();
        List<WorkLog> list = workLogService.selectWorkLogList(workLog);
        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 导出跟踪日志列表
     */
    @PreAuthorize("@ss.hasPermi('system:WorkLog:export')")
    @Log(title = "跟踪日志", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WorkLog workLog)
    {
        List<WorkLog> list = workLogService.selectWorkLogList(workLog);
        ExcelUtil<WorkLog> util = new ExcelUtil<WorkLog>(WorkLog.class);
        return util.exportExcel(list, "WorkLog");
    }

    /**
     * 获取跟踪日志详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:WorkLog:query')")
    @GetMapping(value = "/{workId}")
    public AjaxResult getInfo(@PathVariable("workId") Long workId)
    {
        return AjaxResult.success(workLogService.selectWorkLogById(workId));
    }

    /**
     * 新增跟踪日志
     */
    @PreAuthorize("@ss.hasPermi('system:WorkLog:add')")
    @Log(title = "跟踪日志", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkLog workLog)
    {
        return toAjax(workLogService.insertWorkLog(workLog));
    }

    /**
     * 修改跟踪日志
     */
    @PreAuthorize("@ss.hasPermi('system:WorkLog:edit')")
    @Log(title = "跟踪日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkLog workLog)
    {
        return toAjax(workLogService.updateWorkLog(workLog));
    }

    /**
     * 删除跟踪日志
     */
    @PreAuthorize("@ss.hasPermi('system:WorkLog:remove')")
    @Log(title = "跟踪日志", businessType = BusinessType.DELETE)
	@DeleteMapping("/{workIds}")
    public AjaxResult remove(@PathVariable Long[] workIds)
    {
        return toAjax(workLogService.deleteWorkLogByIds(workIds));
    }

    /**
     * 读码率汇总
     * @param workLog
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:WorkLog:BcrRead')")
    @GetMapping("/BcrRead")
    public TableDataInfo BcrRead(WorkLog workLog)
    {
        startPage();
        List<WorkLog> BcrRead = workLogService.selectBcrRead(workLog);
        return getDataTable(BcrRead);
    }

    /**
     * 出口分拣统计
     * @param workLog
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:WorkLog:LevelSeorting')")
    @GetMapping("/LevelSeorting")
    public TableDataInfo LevelSeorting(WorkLog workLog)
    {
        startPage();
        List<WorkLog> LevelSeorting = workLogService.selectLevelSeorting(workLog);

//        List<PortStatistics> portList = new ArrayList<>();
//        for(WorkLog log : LevelSeorting){
//            PortStatistics port = new PortStatistics();
//            port.setCurPos(log.getCurPos());
//            port.setSum(log.getSum());
//            portList.add(port);
//        }
        return getDataTable(LevelSeorting);
//        return getDataTable(portList);
    }

    /**
     * 时间段流量汇总
     * @param workLog
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:WorkLog:Trafficquery')")
    @GetMapping("/Trafficquery")
    public TableDataInfo Trafficquery(WorkLog workLog)
    {
        startPage();
        List<WorkLog> Trafficquery = workLogService.selectTrafficquery(workLog);
        return getDataTable(Trafficquery);
    }

    /**
     * 异常汇总
     * @param workLog
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:WorkLog:Unusual')")
    @GetMapping("/Unusual")
    public TableDataInfo Unusual(WorkLog workLog)
    {
        startPage();
        List<WorkLog> Unusual = workLogService.selectUnusual(workLog);
        return getDataTable(Unusual);
    }

    /**
     * 异常汇总导出
     * @param workLog
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:WorkLog:Unusualexport')")
    @Log(title = "异常汇总", businessType = BusinessType.EXPORT)
    @GetMapping("/Unusualexport")
    public AjaxResult Unusualexport(WorkLog workLog)
    {
        List<WorkLog> list = workLogService.selectUnusual(workLog);
        ////将数据转到另一个类中
        List<UnusualExport> exportList = new ArrayList<>();
        for(WorkLog log : list){
            UnusualExport unusualExport = new UnusualExport();
            unusualExport.setBcr(log.getBcr());
            unusualExport.setCase1(log.getCase1());
            unusualExport.setCase2(log.getCase2());
            unusualExport.setCase4(log.getCase4());
            unusualExport.setCase6(log.getCase6());
            unusualExport.setCase8(log.getCase8());
            unusualExport.setCase9(log.getCase9());
            unusualExport.setCase10(log.getCase10());
            unusualExport.setCase11(log.getCase11());
            unusualExport.setCase13(log.getCase13());
            unusualExport.setCase28(log.getCase28());
            unusualExport.setCase38(log.getCase38());
            unusualExport.setExceptionSorting(log.getExceptionSorting());
            unusualExport.setNoreadSorting(log.getNoreadSorting());
            unusualExport.setPercentRead(log.getPercentRead());
            unusualExport.setPercentSorting(log.getPercentSorting());
            unusualExport.setSuccessfulSorting(log.getSuccessfulSorting());
            unusualExport.setSumSorting(log.getSumSorting());
            exportList.add(unusualExport);
        }
        ExcelUtil<UnusualExport> util = new ExcelUtil<UnusualExport>(UnusualExport.class);
        return util.exportExcel(exportList, "异常统计");

//        ExcelUtil<WorkLog> util = new ExcelUtil<WorkLog>(WorkLog.class);
//        return util.exportExcel(list, "异常统计");
    }

    /**
     * 导出格口分布列表
     */
    @PreAuthorize("@ss.hasPermi('system:WorkLog:LevelSeortingexport')")
    @Log(title = "格口分布", businessType = BusinessType.EXPORT)
    @GetMapping("/LevelSeortingeExport")
    public AjaxResult LevelSeortingeExport(WorkLog workLog)
    {
        startPage();
        List<WorkLog> LevelSeorting = workLogService.selectLevelSeorting(workLog);

        List<PortStatistics> portList = new ArrayList<>();
        for(WorkLog log : LevelSeorting){
            PortStatistics port = new PortStatistics();
            port.setCurPos(log.getCurPos());
            port.setSum(log.getSum());
            portList.add(port);
        }
        ExcelUtil<PortStatistics> util = new ExcelUtil<PortStatistics>(PortStatistics.class);
        return util.exportExcel(portList, "格口分布");
    }
 /**
     * 导出读码率列表
     */
    @PreAuthorize("@ss.hasPermi('system:WorkLog:BcrReadexport')")
    @Log(title = "读码率", businessType = BusinessType.EXPORT)
    @GetMapping("/BcrReadExport")
    public AjaxResult BcrReadExport(WorkLog workLog)
    {
        startPage();
        List<WorkLog> ReadPerList = workLogService.selectBcrRead(workLog);

        List<ReadPer> readList = new ArrayList<>();
        for(WorkLog log : ReadPerList){
            ReadPer readPer = new ReadPer();
            readPer.setBcr(log.getBcr());
            readPer.setReadSum(log.getReadSum());
            readPer.setNoRead(log.getNoRead());
            readPer.setReadSuccess(log.getReadSuccess());
            readPer.setReadRate(log.getReadRate());
            readList.add(readPer);
        }
        ExcelUtil<ReadPer> util = new ExcelUtil<ReadPer>(ReadPer.class);
        return util.exportExcel(readList, "读码率");
    }

}
